package com.amikomgamedev.labirinhantu.entity;

import java.util.ArrayList;

import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.sprite.AnimatedSprite;

import com.amikomgamedev.labirinhantu.Server_Data;
import com.amikomgamedev.labirinhantu.grafik.Grafik_Anim;

public class Entity_Ghost implements Grafik_Anim
{
	private float	width	= 0
			, height		= 0;
	
	private float[]	start_X	= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
			, start_Y	= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
			, end_X		= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
			, end_Y		= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

	private int	moveDown	= 0
			, moveRight	= 1;
	private int[] ghostMove	= {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
	
	private float[][]	DURATION	= 
		{
        		{
                		2, 2, 2, 2, 6, 5, 2, 2, 
                		2, 2, 3, 10, 2, 8, 
                		2, 2, 2, 5, 9, 5, 
        		}, {
                		2, 3, 5, 6, 6, 6, 2, 2, 
                		2, 4, 2, 4, 2, 2, 
                		2, 4, 5, 5, 2, 2, 
        		}, {
                		2, 2, 2, 2, 
                		5, 2, 2, 
                		2, 2, 2, 
        		}, 
		};
	
	private ArrayList<AnimatedSprite>	m_Ghost;
	private ArrayList<Rectangle>		m_Ghost_Move;
	
	public Entity_Ghost(ArrayList<AnimatedSprite> p_Ghost
			, ArrayList<Rectangle> p_Ghost_Move)
	{
		m_Ghost			= p_Ghost;
		m_Ghost_Move	= p_Ghost_Move;
		
		initMove();
	}
	
	private void initMove()
	{
		for (int i = 0; i < m_Ghost.size(); i++)
			for (int j = 0; j < m_Ghost_Move.size(); j++)
				if (m_Ghost.get(i).collidesWith(m_Ghost_Move.get(j)))
				{
					width	= m_Ghost_Move.get(j).getWidth();
					height	= m_Ghost_Move.get(j).getHeight();
					
					ghostMove[i]	= width > height ? moveRight : moveDown;
					
					if (ghostMove[i] == moveRight)
					{
						m_Ghost.get(i).animate(GAME_PLAY_GHOST_DUR[Server_Data.getStage()], GAME_PLAY_GHOST_FRAME[Server_Data.getStage()][0], true);
						
						start_X[i]	= m_Ghost_Move.get(j).getX();
						end_X[i]	= m_Ghost_Move.get(j).getX() + width - m_Ghost.get(i).getWidth();
						
						start_Y[i]	= end_Y[i]	= m_Ghost.get(i).getY();
					}
					else
					{
						start_X[i]	= end_X[i]	= m_Ghost.get(i).getX();
						start_Y[i]	= m_Ghost_Move.get(j).getY();
						end_Y[i]	= m_Ghost_Move.get(j).getY() + height - m_Ghost.get(i).getHeight();
					}

//					Utils.TRACE("start_X = " +start_X[i]);
//					Utils.TRACE("end_X = " +end_X[i]);
//					Utils.TRACE("start_Y = " +start_Y[i]);
//					Utils.TRACE("end_Y = " +end_Y[i]);
					
					m_Ghost.get(i).registerEntityModifier(
							new LoopEntityModifier(
							new SequenceEntityModifier(
									new MoveModifier(DURATION[Server_Data.getStage()][Server_Data.getMap()]
											, start_X[i], end_X[i]
											, start_Y[i], end_Y[i])
									, new MoveModifier(DURATION[Server_Data.getStage()][Server_Data.getMap()]
											, end_X[i], start_X[i]
											, end_Y[i], start_Y[i]))));
				}
	}
	
	public void checkMove()
	{
		for (int i = 0; i < m_Ghost.size(); i++)
		{
			if (ghostMove[i] == moveRight)
			{
				if (m_Ghost.get(i).getX() <= start_X[i] + height / 15)
				{
//					if (!m_Ghost.get(i).isFlippedHorizontal())
						m_Ghost.get(i).setFlippedHorizontal(true);
				}
				else if (m_Ghost.get(i).getX() >= end_X[i] - height / 15)
				{
//					if (m_Ghost.get(i).isFlippedHorizontal())
						m_Ghost.get(i).setFlippedHorizontal(false);
				}
			}
			else
			{
				if (m_Ghost.get(i).getY() <= start_Y[i] + width / 15)
				{
					m_Ghost.get(i).animate(GAME_PLAY_GHOST_DUR[Server_Data.getStage()]
							, GAME_PLAY_GHOST_FRAME[Server_Data.getStage()][1], true);
				}
				else if (m_Ghost.get(i).getY() >= end_Y[i] - width / 15)
				{
					m_Ghost.get(i).animate(GAME_PLAY_GHOST_DUR[Server_Data.getStage()]
							, GAME_PLAY_GHOST_FRAME[Server_Data.getStage()][3], true);
				}
			}
		}
	}
}
